package cn.circle.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.circle.commom.controller.BackendBaseController;
import cn.circle.system.model.SysMenus;
import cn.circle.system.pagemodel.DataGrid;
import cn.circle.system.pagemodel.PageHelper;
import cn.circle.system.pagemodel.ReturnInfo;
import cn.circle.system.pagemodel.SimpleTreeNode;
import cn.circle.system.service.ISysMenusService;
import cn.circle.system.util.SimpleMenuTreeUtil;

/*import com.linewell.commom.controller.BackendBaseController;
import com.linewell.system.interceptor.BackendInterceptor;
import com.linewell.system.model.SysMenuActions;
import com.linewell.system.model.SysMenus;
import com.linewell.system.pagemodel.DataGrid;
import com.linewell.system.pagemodel.PageHelper;
import com.linewell.system.pagemodel.ReturnInfo;
import com.linewell.system.pagemodel.SimpleTreeNode;
import com.linewell.system.service.ISysMenusService;
import com.linewell.system.utils.SimpleMenuTreeUtil;*/

/**
 * 菜单系统
 * 
 */
@Controller
@RequestMapping("manage/sysMenus")
public class SysMenusController extends BackendBaseController {
	private static final Log log = LogFactory.getLog(SysMenusController.class);
	@Autowired
	private  ISysMenusService sysMenusService;

	/**
	 * 跳转至菜单列表页面
	 */
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request) {
		return super.index("/WEB-INF/pages/system/sys_menus/sys_menus_list.jsp", request);

	}
	/**
	 * 数据查询
	 */
	@ResponseBody
	@RequestMapping(value = "/dataGrid")
	public DataGrid dataGrid(PageHelper ph) {

		ph.setSort("showOrder");
		ph.setOrder("desc");
		DataGrid dg = sysMenusService.datagrid(ph);
		return dg;
	}
	/**
	 * 添加
	 */
	@RequestMapping(value = "/add")
	public String add(Map<String, Object> map) {
		map.put("saveUrl", "/manage/sysMenus/addSave");
		return "/WEB-INF/pages/system/sys_menus/sys_menus_au.jsp";
	}

	/**
	 * 获取添加菜单时所需要的模块信息
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/getModule")
	public ReturnInfo getModule(Integer type, ReturnInfo info) {
		List<SimpleTreeNode> lstTree = null;
		if (type != null && !"".equals(type)) {

			try {
				List<SysMenus> lst = sysMenusService.getMenus(type);
				if (lst != null && lst.isEmpty() == false) {
					SimpleMenuTreeUtil treeUtil = new SimpleMenuTreeUtil();
					treeUtil.setLstAll(lst);
					lstTree = treeUtil.findById(null);
				}

				info.setObj(lstTree);

			} catch (Exception e) {
				log.info("异常" + e.getMessage());
			}

		}
		return info;
	}
	/**
	 * 添加保存
	 */
	@ResponseBody
	@RequestMapping(value = "/addSave")
	public ReturnInfo addSave(@Valid SysMenus sysMenus, BindingResult b, ReturnInfo result) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (sysMenus != null) {
 
				try {
					sysMenusService.save(sysMenus);
				} catch (Exception e) {
					log.info(e.getMessage());
					result.setMsg("保存失败！");
					result.setSuccess(1);
				}

			}
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/edit")
	public String edit(String id, Map<String, Object> map) {
		log.info(id);
		map.put("saveUrl", "/manage/sysMenus/editSave");
		if (id != null) {
			List<SysMenus> model = sysMenusService.get(id);
			map.put("model", model.get(0));
		}
		return "/WEB-INF/pages/system/sys_menus/sys_menus_au.jsp";
	}

	/**
	 * 修改保存
	 */
	@ResponseBody
	@RequestMapping(value = "/editSave")
	public ReturnInfo editSave(@Valid SysMenus sysMenus, BindingResult b, ReturnInfo result) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				result.setMsg(error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (sysMenus != null) {

				try {
					sysMenusService.update(sysMenus);
					//sysMenusService.saveOrUpdate(sysMenus);
				} catch (Exception e) {
					log.info(e.getMessage());
					result.setMsg("保存失败！");
					result.setSuccess(1);
				}

			}
		}

		return result;
	}

	/**
	 * 删除（物理删除）
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public ReturnInfo delete(String id, ReturnInfo info) {

		log.info("删除id" + id);
		if (id != null) {
			try {
				sysMenusService.removeAll(id);
			} catch (Exception e) {
				log.info("删除异常" + e.getMessage());
				info.setMsg("删除失败！");
				info.setSuccess(1);
			}
		}
		return info;
	}

	/**
	 * 启用菜单
	 */
	@ResponseBody
	@RequestMapping(value = "/openMenu")
	public ReturnInfo openMenu(String id, ReturnInfo info) {
		if (id != null) {	
			try {
				sysMenusService.openMenu(id);
			} catch (Exception e) {
				log.info("启用异常" + e.getMessage());
				info.setMsg("启用失败！");
				info.setSuccess(1);
			}
		}
		return info;
	}

	/**
	 * 禁用菜单
	 */
	@ResponseBody
	@RequestMapping(value = "/closeMenu")
	public ReturnInfo closeMenu(String id, ReturnInfo info) {
		if (id != null) {		
			try {
				sysMenusService.closeMenu(id);
			} catch (Exception e) {
				log.info("禁用异常" + e.getMessage());
				info.setMsg("禁用失败！");
				info.setSuccess(1);
			}
		}
		return info;
	}

	/**
	 * 跳转至 修改菜单action页面
	 */
/*	@RequestMapping(value = "/actionUrl")
	public String actionUrl(String id, Map<String, Object> map) {
		log.info(id);
		map.put("id", id);
		if (id != null) {
			List<SysMenuActions> model = sysMenusService.getAction(id);
			map.put("actions", model);
		}
		return "/WEB-INF/pages/system/sys_menus/sys_menus_action.jsp";
	}*/
	/**
	 * 保存修改的菜单action
	 */
	/*@ResponseBody
	@RequestMapping(value = "/actionSave")
	public ReturnInfo actionSave(Integer id, String action_url, ReturnInfo info) {
		String urls[] = action_url.split(",");
		if (id > 0) {

			try {
				sysMenusService.delete(id);
			} catch (Exception e) {
				log.info("删除异常" + e.getMessage());
				info.setMsg("删除失败！");
				info.setSuccess(1);
			}
		}
		for (String url : urls) {
			if (!"".trim().equals(url)) {

				try {
					BackendInterceptor.addMenu(url);
					sysMenusService.saveUrl(url.trim(), id);
				} catch (Exception e) {
					log.info("添加异常" + e.getMessage());
					info.setMsg("添加失败！");
					info.setSuccess(1);
				}

			}
		}
		return info;
	}*/
	/**
	 * 获取保存权限需要的json数据
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/getPermissModule")
	public List<SimpleTreeNode> getPermissModule(Integer type) {
		List<SimpleTreeNode> lstTree = null;
		if (type != null && !"".equals(type)) {

			try {
				List<SysMenus> lst = sysMenusService.getMenus(type);
				if (lst != null && lst.isEmpty() == false) {
					SimpleMenuTreeUtil treeUtil = new SimpleMenuTreeUtil();
					treeUtil.setLstAll(lst);
					lstTree = treeUtil.findById(null);
				}

			} catch (Exception e) {
				log.info("异常" + e.getMessage());
			}

		}
		return lstTree;
	}	
}
